<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>增强型IP黑名单管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <blockquote class="layui-elem-quote layui-text">
            帮助中心:1. <a href="#" target="_blank">IP黑名单帮助中心</a>
            <a class="layui-btn" style="float:right" href="javascript:location.replace(location.href);" title="刷新">
              <i class="layui-icon">&#xe669;</i></a>
        </blockquote>
        <fieldset class="table-search-fieldset">
            <legend>搜索</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">应用名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="appname" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">黑名单IP</label>
                            <div class="layui-input-inline">
                                <input type="text" name="ip" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <table class="layui-table" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="Add"><i class="layui-icon">&#xe61f;</i>添加黑名单IP</button>
            </div>
        </script>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
    
    	<!-- 弹添加框-->
  	<div class="layui-row" id="AddRuleInfo" style="display:none;margin: 40px 40px 40px 40px">
        <blockquote class="layui-elem-quote layui-text">
            <span style="color:red">IP黑名单添加说明:</span><br>1.应用范围包含三个级别,sys:表示应用到全网,pcauto:应用到汽车网应用,pcauot-bbs:只应用到汽车网论坛<br>
            2.黑名单ip：可以批量添加，一个IP一行。<br>3.备注：标记此次添加IP的用意或IP出处。<br>4.备注：封禁时长0时表示永久封禁。
        </blockquote>
          <form class="layui-form layui-form-pane" action="">
              <div class="layui-form-item" pane="">
                  <div class="layui-inline">
                      <label class="layui-form-label">应用范围</label>
                      <div class="layui-input-inline" style="width:400px">
                          <input type="text" name="appname" autocomplete="off" class="layui-input" placeholder="分三个级别,例如:sys,pcauto,pcauto-bbs" lay-verify="required" lay-reqtext="应用名不能为空">
                      </div>
                  </div>
              </div>
              <div class="layui-form-item" pane="">
                  <div class="layui-inline">
                      <label class="layui-form-label">黑名单ip</label>
                      <div class="layui-input-block">
                          <textarea name="ip" style="height:147px;width:400px" class="layui-textarea"  lay-verify="required" lay-reqtext="IP不能为空" placeholder="请输入ip，一行一个,内网IP谨慎添加，参考格式:&#13;&#10;192.168.1.1&#13;&#10;192.168.1.2"></textarea>
                      </div>
                  </div>
              </div>
              <div class="layui-form-item" pane="">
                  <div class="layui-inline">
                      <label class="layui-form-label">封禁时长</label>
                      <div class="layui-input-inline" style="width:400px">
                          <input type="text" name="expiretime" autocomplete="off" value="0"  placeholder="封禁多少秒" class="layui-input">
                      </div>
                      <div class="layui-form-mid layui-word-aux">秒,0表示永久封禁</div>
                  </div>
              </div>
              <div class="layui-form-item" pane="">
                  <div class="layui-inline">
                      <label class="layui-form-label">备注</label>
                      <div class="layui-input-inline" style="width:400px">
                          <input type="text" name="remarks" autocomplete="off" class="layui-input">
                      </div>
                  </div>
              </div>
              <div class="layui-form-item">
                  <div class="layui-inline" style="margin-left:200px">
                      <button type="submit" class="layui-btn"  lay-submit lay-filter="Add"><i class="layui-icon">&#xe61f;</i>确认添加</button>
                  </div>
              </div>
          </form>
  	</div>
	<!-- 弹添加框end-->  
    
</div>
<script type="text/html" id="statusTpl">
  <input type="checkbox" name="status" value="{{d.id}},{{d.appname}}" lay-skin="switch" lay-text="禁止|允许" lay-filter="blackIpStatus" {{ d.status == 1 ? 'checked' : '' }}>
</script>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    function getQueryString(name) {
      let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
      let r = window.location.search.substr(1).match(reg);
      if (r != null) {
          return unescape(r[2]);
      };
      return null;
    };
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

//var stype = getQueryString("ruletype");

//修改ip状态
      //table里状态
      form.on('switch(blackIpStatus)', function(data){
        var vs = 1;
        if (this.checked) {
          vs = 1;
        }else{
          vs = 0;
        }
        var urls = location.search;
	      if (urls == "") {
		      urls = urls.replace("?", "&");
	      }
        var arry = this.value.split(',')
        var id = arry[0];
        var appname = arry[1];
        var bodystr = {"appname":appname,"name":this.name,"value":vs};
    			  $.ajax({
    			   url: "/xwaf/blackip/v1/Edit?id="+id + urls,
    			   headers:{'X-Requested-With':'XMLHttpRequest'},
             type: "POST",
             data: JSON.stringify(bodystr),
             dataType: "json",
    			   success: function (resp) {
    					  if (resp.code == 0 ) {
                  layer.msg('更改成功', {icon: 1});
                }else{
                  layer.msg(resp.msg,{icon:2});
                }
    				  }
    		    });
      });

        table.render({
            elem: '#currentTableId',
            url: '/xwaf/blackip/v1/List'+location.search,
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter'],
            cols: [[
                //{type: "checkbox", width: 50},
                {field: 'id',width:60,  title: 'ID', sort: true},
                {field: 'appname', title: '应用名',edit:'text'},
                {field: 'ip',width:'40%',edit:'text', title: 'IP', sort: true},
                {field: 'status',title: '状态',templet: '#statusTpl'},
                {field: 'expiretime',title: '封禁时长',edit:'text',sort: true},
                {field: 'remarks',title: '备注',edit:'text'},
                {field: 'createtime', title: '创建时间', hide:true},
                {field: 'updatetime', title: '时间'},
                {title: '操作', minWidth:120, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [25, 50, 100],
            limit: 15,
            page: true
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            //layer.alert(result, {
            //    title: '最终的搜索信息'
            //});
            data.field.appname = data.field.appname.replace(/^\s*|\s*$/g,"");
            data.field.ip = data.field.ip.replace(/^\s*|\s*$/g,"");
            //执行搜索重载
            table.reload('currentTableId', {
                //page: {
                //    curr: 1,
                //    limit:50
                //},
                method:'POST',
                url: '/xwaf/blackip/v1/List',
                contentType: 'application/json',
                where: data.field,
                done: function(res, curr, count){
                  //console.log(res);
                  //this.where={};
                  //不清空，有问题：搜索条件会保留
                  //清空，  有问题：分页的时候没有搜索条件了,分页会受到影响
                  //https://copyfuture.com/blogs-details/20200729003153292ltjtf01kvp048gr
                }
            }, 'data');

            return false;
        });

  	//监听单元格编辑
  	table.on('edit(currentTableFilter)', function(obj){
    	var value = obj.value //得到修改后的值
    	,datas = obj.data //得到所在行所有键值
    	,field = obj.field; //得到字段

      var bodystr = {"appname":datas.appname,"id":datas.id,"name":field,"value":value};
            var title = '确定修改?'
            if (field == "ip") {
              title = '修改后,如果源ip状态是禁用,需要源IP过期或重启nginx后才会生效,确定修改?'
            }
			      layer.confirm(title, {icon: 3, title:'提示',yes: function(index){
                 	$.ajax({
                     	url: "/xwaf/blackip/v1/Edit?id="+datas.id,
                      type: "POST",
                      data: JSON.stringify(bodystr),
                      dataType: "json",
                     	success: function (resp) {
                         	if(resp.code == 0){
                            	layer.msg('['+ datas.id +'] ' + field + '更改为:'+ value);
                         	}else{
                            	layer.msg('['+ datas.id +'] ' + field + '更改失败',{icon:2});
                         	}
                    	}
                	});	
              },
              cancel: function(index, layero){ 
                  layer.close(index);
              } 
            }); 	
  	});
    
  	table.on('toolbar(currentTableFilter)', function(obj){
    	//var checkStatus = table.checkStatus(obj.config.id);
    	switch(obj.event){
      		case 'Add':
					layer.open({
            			type:1,
						      skin: 'layui-layer-rim',
            			title:"添加黑名单IP",
            			area: ['50%', '70%'],
            			content:$("#AddRuleInfo").html(),
                  end: function(){
                  	window.location.reload();//关闭open打开的页面时，刷新父页面
                  }
        	});
          form.render();	
      		break;									
    	};
  	});
  
    //添加
    form.on('submit(Add)', function(data){		
          data.field.appname = data.field.appname.replace(/^\s*|\s*$/g,"");
          data.field.ip = data.field.ip.replace(/：/g,":");
          data.field.ip = data.field.ip.replace(/，|\r|\n/g,",");
          data.field.ip = data.field.ip.replace(/^\s*|\s*$/g,"");
          data.field.ip = data.field.ip.replace(/\s/g,"");
		      console.log(JSON.stringify(data.field));      
          $.ajax({
            url: "/xwaf/blackip/v1/JudgeWhiteListIp?ip="+data.field.ip,
            type: "post",
            data: JSON.stringify(data.field),
            dataType: "json",
            success: function (resp) {
                console.log(resp);
                if(resp.code == 0){                   
          			    layer.confirm('有正常IP,确定添加?', {icon: 2, title:resp.msg,yes: function(index){
                               $.ajax({
                                 url: "/xwaf/blackip/v1/Add"+location.search,
                                 type: "post",
                                 data: JSON.stringify(data.field),
                                 dataType: "json",
                                 success: function (resp) {
          					                console.log(resp);
                                     if(resp.code == 0){
                                        layer.msg('添加成功', {icon: 1});  
                                     }else{
                                         layer.msg('添加失败',{icon:2});
                                     }
                                  }
                               });		  	  
                         },
                          cancel: function(index, layero){ 
                              layer.close(index);
                          } 
                    }); 
                }else{
          			    layer.confirm('确定添加?', {icon: 3, title:'提示',yes: function(index){
                               $.ajax({
                                 url: "/xwaf/blackip/v1/Add"+location.search,
                                 type: "post",
                                 data: JSON.stringify(data.field),
                                 dataType: "json",
                                 success: function (resp) {
          					                console.log(resp);
                                     if(resp.code == 0){
                                        layer.msg('添加成功', {icon: 1});  
                                     }else{
                                         layer.msg('添加失败',{icon:2});
                                     }
                                  }
                               });		  	  
                         },
                          cancel: function(index, layero){ 
                              layer.close(index);
                          } 
                    });               
                }
              }  		 
		      }); 
		      return false; 
		});	

        //监听表格复选框选择
        //table.on('checkbox(currentTableFilter)', function (obj) {
         //   console.log(obj)
        //});

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'delete') {
                //console.log(data);
                layer.confirm('确定删除'+data.id+'?', {icon: 3, title:'删除提示',yes: function(index){
                     var index2 = layer.load(0, {shade: false});
                     layer.msg('请稍等...');
                			 $.ajax({
                			    url: "/xwaf/blackip/v1/Del?id="+data.id+"&appname="+data.appname,
                			    type: "get",
                			    success: function (resp) {
                            layer.close(index2);

                						 if(resp.code == 0){
                							 layer.msg('删除成功', {icon: 1});
                							 //window.location.reload();
                							 obj.del();
                						 }else{
                							 layer.msg(resp.msg,{icon:2});
                						 }
                				  }
                		   });		  	  
                },
            		  cancel: function(index, layero){ 
            			    layer.close(index);
                      layer.close(index2);
            			    console.log("不操作");
            		  } 
        		    });                
            }
        });
    });
</script>

</body>
</html>